﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MY.WCF.Client
{
    public partial class Calculator : Form
    {
        string tmpInputStr = string.Empty;
        string tmpResultStr = string.Empty;
        Operation CurOperation;

        public Calculator()
        {
            InitializeComponent();//初始化designer时拖到Form上的Control
        }

        private void Num1_Click(object sender, EventArgs e)
        {
            tmpInputStr += "1";
            this.ResultBox.Text = tmpInputStr;
        }

        private void Num2_Click(object sender, EventArgs e)
        {
            tmpInputStr += "2";
            this.ResultBox.Text = tmpInputStr;
        }

        private void Num3_Click(object sender, EventArgs e)
        {
            tmpInputStr += "3";
            this.ResultBox.Text = tmpInputStr;
        }

        private void Num4_Click(object sender, EventArgs e)
        {
            tmpInputStr += "4";
            this.ResultBox.Text = tmpInputStr;
        }

        private void Num5_Click(object sender, EventArgs e)
        {
            tmpInputStr += "5";
            this.ResultBox.Text = tmpInputStr;
        }

        private void Num6_Click(object sender, EventArgs e)
        {
            tmpInputStr += "6";
            this.ResultBox.Text = tmpInputStr;
        }

        private void Num7_Click(object sender, EventArgs e)
        {
            tmpInputStr += "7";
            this.ResultBox.Text = tmpInputStr;
        }

        private void Num8_Click(object sender, EventArgs e)
        {
            tmpInputStr += "8";
            this.ResultBox.Text = tmpInputStr;
        }

        private void Num9_Click(object sender, EventArgs e)
        {
            tmpInputStr += "9";
            this.ResultBox.Text = tmpInputStr;
        }

        private void Num0_Click(object sender, EventArgs e)
        {
            tmpInputStr += "0";
            this.ResultBox.Text = tmpInputStr;
        }

        private void ButtonEqual_Click(object sender, EventArgs e)
        {
            using (ServiceReference.CalculatorServiceClient clientProxy = new ServiceReference.CalculatorServiceClient())
            {
                switch (this.CurOperation)
                {
                    case Operation.Add:
                        this.ResultBox.Text = clientProxy.Add(double.Parse(this.tmpResultStr), double.Parse(this.tmpInputStr)).ToString();
                        this.tmpResultStr = this.ResultBox.Text;
                        this.tmpInputStr = string.Empty;
                        break;
                    case Operation.Subtract:
                        this.ResultBox.Text = clientProxy.Subtract(double.Parse(this.tmpResultStr), double.Parse(this.tmpInputStr)).ToString();
                        this.tmpResultStr = this.ResultBox.Text;
                        this.tmpInputStr = string.Empty;
                        break;
                    case Operation.Multiply:
                        this.ResultBox.Text = clientProxy.Multiply(double.Parse(this.tmpResultStr), double.Parse(this.tmpInputStr)).ToString();
                        this.tmpResultStr = this.ResultBox.Text;
                        this.tmpInputStr = string.Empty;
                        break;
                    case Operation.Divide:
                        this.ResultBox.Text = clientProxy.Divide(double.Parse(this.tmpResultStr), double.Parse(this.tmpInputStr)).ToString();
                        this.tmpResultStr = this.ResultBox.Text;
                        this.tmpInputStr = string.Empty;
                        break;
                }
            }
        }

        private void ButtonAdd_Click(object sender, EventArgs e)
        {
            this.CurOperation = Operation.Add;
            if (string.IsNullOrEmpty(this.tmpResultStr))
            {
                this.tmpResultStr = this.tmpInputStr;
            }
            this.tmpInputStr = string.Empty;
        }

        private void ButtonSubtract_Click(object sender, EventArgs e)
        {
            this.CurOperation = Operation.Subtract;
            if (string.IsNullOrEmpty(this.tmpResultStr))
            {
                this.tmpResultStr = this.tmpInputStr;
            }
            this.tmpInputStr = string.Empty;
        }

        private void ButtonMultiply_Click(object sender, EventArgs e)
        {
            this.CurOperation = Operation.Multiply;
            if (string.IsNullOrEmpty(this.tmpResultStr))
            {
                this.tmpResultStr = this.tmpInputStr;
            }
            this.tmpInputStr = string.Empty;
        }

        private void ButtonDivide_Click(object sender, EventArgs e)
        {
            this.CurOperation = Operation.Divide;
            if (string.IsNullOrEmpty(this.tmpResultStr))
            {
                this.tmpResultStr = this.tmpInputStr;
            }
            this.tmpInputStr = string.Empty;
        }

        private void ButtonClear_Click(object sender, EventArgs e)
        {
            this.tmpResultStr = string.Empty;
            this.tmpInputStr = string.Empty;
            this.ResultBox.Text = string.Empty;
        }

        private void ButtonSymbol_Click(object sender, EventArgs e)
        {
            if (this.ResultBox.Text.IndexOf("-") == 0)
            {
                this.ResultBox.Text = this.ResultBox.Text.Replace("-",string.Empty);
                this.tmpInputStr = tmpInputStr.Replace("-", string.Empty);
            }
            else
            {
                this.ResultBox.Text = "-" + this.ResultBox.Text;
                this.tmpInputStr = "-" + tmpInputStr;
            }
        }
    }

    enum Operation : int
    {
        Add = 1,
        Subtract = 2,
        Multiply = 3,
        Divide = 4
    }
}
